<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\UpdateOncustomer;
use App\Models\Deposit;
use App\Models\Oncustomer;
use App\Models\OncustomerTarget;
use App\Models\Onorder;
use App\Models\Store;
use App\Models\Companies;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class OncustomerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */


    public function __construct()
    {
        view()->share([
            '_customers' => 'am-in',
            '_oncustomer' => 'am-active',

        ]);

    }

    //会员管理首页
    public function index(Request $request)
    {
        $where = function ($query) use ($request) {
            //按openid
            if ($request->has('openid') and $request->openid != '') {
                $search = "%" . $request->openid . "%";
                $query->where('openid', 'like', $search);
            }
            //按会员姓名
            if ($request->has('name') and $request->name != '') {
                $search = "%" . $request->name . "%";
                $query->where('name', 'like', $search);
            }
            //按会员电话
            if ($request->has('tel') and $request->tel != '') {
                $search = "%" . $request->tel . "%";
                $query->where('tel', 'like', $search);
            }
            //按门店
            if ($request->has('store_id') and $request->store_id != '-1') {

                $query->where('store_id', $request->store_id);
            }
            if ($request->has('company_id') and $request->company_id != '') {

                $query->where('company_id', $request->company_id);
            }


        };
        //所有门店
        $cates = Store::all();
        //会员关联门店，公司
        $customers = Oncustomer::with('store')->with('company')->where($where)->orderBy('created_at', 'desc')->paginate(15);

        return view('admin.oncustomer.index', compact('customers', 'cates'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    //充值页面
    public function edit($id)
    {
        //当前会员
        $customer = Oncustomer::find($id);
        return view('admin.oncustomer.edit', compact('customer'));
    }

    //执行充值并记录在表
    public function update(UpdateOncustomer $request, $id)
    {
        //当前会员
        $customer = Oncustomer::find($id);
        //执行充值
        $customer->card_money = $customer->card_money + $request->deposit_number;
        $customer->save();
        $data['deposit_number'] = $request->deposit_number;
        $data['contro'] = $request->contro;
        $data['type'] = 0;
        $data['oncustomer_id'] = $id;
        $data['store_id'] = $customer->store_id;
        $data['statu'] = 0;
        $data['is_pay'] = 1;
        $data['oncustomer_name'] = $customer->name;
        $data['oncustomer_tel'] = $customer->tel;
        $data['order_no'] = make_order_no(time());


        //记录充值
        Deposit::create($data);
        return redirect('admin/oncustomer')->with('success', '充值成功');


    }

    //bmi变化
    public function record($id)
    {


//        $time = [
//
//
//            $bmis[0]['record_at'], $bmis[1]['record_at'], $bmis[2]['record_at'], $bmis[3]['record_at'], $bmis[4]['record_at'], $bmis[5]['record_at'], $bmis[6]['record_at'],
//            $bmis[7]['record_at'], $bmis[8]['record_at'], $bmis[9]['record_at'], $bmis[10]['record_at'], $bmis[11]['record_at'], $bmis[12]['record_at'], $bmis[13]['record_at'],
//            $bmis[14]['record_at'], $bmis[15]['record_at'], $bmis[16]['record_at'], $bmis[17]['record_at'], $bmis[18]['record_at'], $bmis[19]['record_at']
//        ];
//        $bmi = [
//            $bmis[0]['mbi'], $bmis[1]['mbi'], $bmis[2]['mbi'], $bmis[3]['mbi'], $bmis[4]['mbi'], $bmis[5]['mbi'], $bmis[6]['mbi'],
//            $bmis[7]['mbi'], $bmis[8]['mbi'], $bmis[9]['mbi'], $bmis[10]['mbi'], $bmis[11]['mbi'], $bmis[12]['mbi'], $bmis[13]['mbi'],
//            $bmis[14]['mbi'], $bmis[15]['mbi'], $bmis[16]['mbi'], $bmis[17]['mbi'], $bmis[18]['mbi'], $bmis[19]['mbi']
//        ];


        return view('admin.oncustomer.record',compact('id'));


    }




    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */


    //待完善，需判断会员时候还有私教，挑战
    public function destroy($id)
    {
        $info = Onorder::where('oncustomer_id', $id)->where('statu', 0)->first();
        if ($info) {
            return back()->with('success', '当前用户有卷码未使用无法删除');
        } else {
            Oncustomer::destroy($id);
            return back()->with('success', '删除成功');
        }

    }

    //AJax修改会员性别
    public function change_attr(Request $request)
    {

        $oncustomer = Oncustomer::find($request->id);

        if ($oncustomer->sex == 0) {
            $data['sex'] = 1;
            $oncustomer->update($data);
        } else {
            $data['sex'] = 0;
            $oncustomer->update($data);
        }
    }

//解除公司绑定
    public function change_company(Request $request)
    {

        $oncustomer = Oncustomer::find($request->id);
        $companyId=$oncustomer['company_id'];
        $oncustomer->company_id=0;
        $result=$oncustomer->save();
        if($result){
            $select=Companies::where('id',$companyId)->first();
            $update=Companies::where('id',$companyId)->update(['rzpeople'=>$select['rzpeople']-1]);
        }

    }

    //是否将用户设置为员工
    public function change_yuangong(Request $request)
    {

        $oncustomer = Oncustomer::find($request->id);
        if ($oncustomer->is_yuangong == 0) {
            $data['is_yuangong'] = 1;
            dd($oncustomer->update($data));
        } else {
            $data['is_yuangong'] = 0;
            $oncustomer->update($data);
        }
    }






}
